package com.example.javaeefinal.Mapper;

import com.example.javaeefinal.pojo.Department;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DepartmentMapper {

    //    提交表单创建新部门
    @Insert("insert into t_department(F_name,F_principal) values (#{name},#{principal})")
    public void createNewDepartment(String name, int principal);

    @Select("select F_id AS id from t_department ORDER BY F_id DESC limit 1;")
    public int selectLastCreateDepartmentId();

    @Insert("insert into t_depInfo(f_id) VALUES (#{id});")
    public void initNewDepInfo(int id);

    //    根据id删除部门
    @Delete("DELETE FROM t_department, t_depInfo\n" +
            "WHERE t_department.F_id = t_depInfo.F_id AND t_department.F_id = #{id};")
    public void deleteDepartmentById(int id);


    @Select("SELECT\n" +
            "    t_department.F_id AS id,\n" +
            "    t_department.F_name AS departmentName,\n" +
            "    t_department.F_principal AS principal,\n" +
            "    t_depInfo.F_mail AS mail,\n" +
            "    t_depInfo.F_address AS address,\n" +
            "    t_depInfo.F_code AS code\n" +
            "FROM\n" +
            "    t_department\n" +
            "        JOIN\n" +
            "    t_depInfo ON t_department.F_id = #{id} AND t_department.F_id = t_depInfo.F_id; ")
    public Department selectDepartmentById(int id);

    @Update("update t_department set F_name = #{departmentName}, F_principal = #{principal} where F_id = #{id}")
    public void updateDepartmentById(int id, String departmentName, int principal);

    @Update("update t_department set F_mail = #{mail}, F_address = #{address}, F_code = #{code} where F_id = #{id}")
    void updateDepInfoById(int id, String mail, String address, String code);

    @Select("SELECT d.F_id AS id, d.F_name AS departName, d.F_principal AS principal, di.F_mail AS mail, di.F_address AS address, di.F_code AS code\n" +
            "FROM t_department d\n" +
            "         JOIN t_depinfo di ON d.F_id = di.F_id;")
    List<Department> getAllDepartment();
}
